import 'package:dark_notes/models/notemodel.dart';
import 'package:dark_notes/models/toast.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class NewNotePage extends StatefulWidget {
  final String title;
  final String action;
  final Note note;
  NewNotePage({this.title, this.action, this.note});

  @override
  _NewNotePageState createState() => _NewNotePageState();
}

class _NewNotePageState extends State<NewNotePage> {
  String _title;
  String _body;

  void _updateTitle(String title) {
    setState(() {
      this._title = title;
    });
  }

  void _updateBody(String body) {
    setState(() {
      this._body = body;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        title: Text(widget.title),
      ),
      body: Container(
        padding: EdgeInsets.all(15),
        child: ListView(
          children: <Widget>[
            Row(children: <Widget>[
              Expanded(
                  child: Text(
                '标题',
                style: Theme.of(context).textTheme.headline5,
              )),
            ]),
            TextFormField(
              decoration: InputDecoration(hintText: '请输入笔记标题'),
              maxLines: null,
              maxLength: 90,
              onChanged: (String value) {
                this._updateTitle(value);
              },
              initialValue: widget.action == 'edit' ? widget.note.title : null,
            ),
            SizedBox(
              height: 15,
            ),
            Row(
              children: <Widget>[
                Expanded(
                    child: Text(
                  '内容',
                  style: Theme.of(context).textTheme.headline5,
                ))
              ],
            ),
            TextFormField(
              decoration: InputDecoration(
                border: InputBorder.none,
                disabledBorder: InputBorder.none,
                enabledBorder: InputBorder.none,
                errorBorder: InputBorder.none,
                focusedBorder: InputBorder.none,
                focusedErrorBorder: InputBorder.none,
                hintText: '请输入笔记内容~',
              ),
              maxLines: null,
              onChanged: (String value) {
                this._updateBody(value);
              },
              initialValue: widget.action == 'edit' ? widget.note.body : null,
            ),
            SizedBox(
              height: 15,
            ),
            // Row(
            //   mainAxisAlignment: MainAxisAlignment.end,
            //   children: <Widget>[
            //     RaisedButton(
            //       child: Text('Submit'),
            //       onPressed: () {
            //         if (this._title == null) {
            //           if (widget.note != null) {
            //             this._title = widget.note.title;
            //           } else {
            //             this._title = '';
            //             return Toast.toast(context, msg: "标题不能为空~");
            //           }
            //         }
            //         if (this._body == null) {
            //           if (widget.note != null) {
            //             this._body = widget.note.body;
            //           } else {
            //             this._body = '';
            //             return Toast.toast(context, msg: "描述不能为空~");
            //           }
            //         }
            //         Note newNote = Note(title: this._title, body: this._body);
            //         if (widget.action == 'new') {
            //           Provider.of<NoteModel>(context, listen: false)
            //               .add(newNote);
            //         } else if (widget.action == 'edit') {
            //           Provider.of<NoteModel>(context, listen: false)
            //               .edit(widget.note, this._title, this._body);
            //         }
            //         Navigator.pop(context);
            //       },
            //     )
            //   ],
            // )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.save),
        onPressed: () {
          if (this._title == null) {
            if (widget.note != null) {
              this._title = widget.note.title;
            } else {
              this._title = '';
              return Toast.toast(context, msg: "标题不能为空~");
            }
          }
          if (this._body == null) {
            if (widget.note != null) {
              this._body = widget.note.body;
            } else {
              this._body = '';
              return Toast.toast(context, msg: "描述不能为空~");
            }
          }
          Note newNote = Note(title: this._title, body: this._body);
          if (widget.action == 'new') {
            Provider.of<NoteModel>(context, listen: false).add(newNote);
          } else if (widget.action == 'edit') {
            Provider.of<NoteModel>(context, listen: false)
                .edit(widget.note, this._title, this._body);
          }
          Navigator.pop(context);
        },
      ),
    );
  }
}
